<?php
namespace app\service;
use think\facade\Db;
class strangerApplyService{
    public function getInfo($id){
        return Db::name('stranger_apply')->where('id',$id)->find();
    }
    public function userAllData($community_id,$page,$where){
        return Db::name('stranger_apply')->where('community_id','in',$community_id)->where($where)->order('id desc')->select();

    }
    public function userList($community_id,$page,$where){
        return Db::name('stranger_apply')->where('community_id','in',$community_id)->where($where)->order('id desc')->paginate(200, false, [
            'page'  =>  $page,
            'query' =>  $where
        ]);

    }
    public function userCount($community_id,$where){
        return Db::name('stranger_apply')->where('community_id','in',$community_id)->where($where)->count();
    }
    public function audit($applyInfo,$staff,$type,$sms_result){
        $add["community_id"]=$applyInfo["community_id"];
        $add["community_name"]=$applyInfo["community_name"];
        $add["user_id"]=$applyInfo["user_id"];
        $add["apply_id"]=$applyInfo["id"];
        $add["device_id"]=$applyInfo["device_id"];
        $add["device_name"]=$applyInfo["device_name"];
        $add["mobile"]=$applyInfo["mobile"];
        $add["name"]=$applyInfo["name"];
        $add["createAt"]=date('Y-m-d H:i:s');

        $add["staff_id"]=$staff["id"];
        $add["staff_name"]=$staff["name"];
        $add["staff_mobile"]=$staff["mobile"];

        $add["user_type"]=$type;

        Db::startTrans();
        try {
            Db::table('stranger_user')->insert($add);
            Db::table('stranger_apply')
                ->where("id",$applyInfo["id"])
                ->update(["status"=>2,"sms_result"=>$sms_result]);
            // 提交事务
            Db::commit();
            return true;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return false;
        }
    }
}